<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of event
 *
 * @author vudp
 */
class feed_Component_Block_Form_Event extends Phpfox_Component{
    //put your code here
    public function process(){

                Phpfox::isUser(true);
		Phpfox::getUserParam('event.can_create_event', true);

		Phpfox_Error::setDisplay(false);
		$bIsEdit = false;
		$sStep = $this->request()->get('step', false);
		$sAction = $this->request()->get('req3', false);
		$aCallback = false;
		$sModule = $this->request()->get('module', false);
		$iItem =  $this->request()->getInt('item', false);
        	if ($iEditId = $this->request()->get('id'))
		{
			if (($aEvent = Phpfox::getService('event')->getForEdit($iEditId)))
			{
				$bIsEdit = true;
				$this->setParam('aEvent', $aEvent);
				$this->setParam(array(
						'country_child_value' => $aEvent['country_iso'],
						'country_child_id' => $aEvent['country_child_id']
					)
				);
				$this->template()->setHeader(array(
							'<script type="text/javascript">$(function(){ var aCategories = explode(\',\', \'' . $aEvent['categories'] . '\'); for (i in aCategories) { $(\'#js_mp_holder_\' + aCategories[i]).show(); $(\'#js_mp_category_item_\' + aCategories[i]).attr(\'selected\', true); } });</script>'
						)
					)
					->assign(array(
						'aForms' => $aEvent,
						'aEvent' => $aEvent
					)
				);

				if ($aEvent['module_id'] != 'event')
				{
					$sModule = $aEvent['module_id'];
					$iItem = $aEvent['item_id'];
				}
			}
		}

		if ($sModule && $iItem && Phpfox::hasCallback($sModule, 'addEvent'))
		{
			$aCallback = Phpfox::callback($sModule . '.addEvent', $iItem);
			if ($bIsEdit === false)
			{
				$this->template()->assign('aForms', $aCallback);
			}

			if ($aCallback !== false)
			{
				$this->setParam('aCallback', $aCallback);
			}
		}

		$aValidation = array(
			'title' => Phpfox::getPhrase('event.provide_a_name_for_this_event'),
			'country_iso' => Phpfox::getPhrase('event.provide_a_country_location_for_this_event'),
			'location' => Phpfox::getPhrase('event.provide_a_location_for_this_event')
		);

		if ($aCallback === false)
		{
			$aValidation['host'] = Phpfox::getPhrase('event.provide_a_host_for_this_event');
		}

		$oValidator = Phpfox::getLib('validator')->set(array(
				'sFormName' => 'js_event_form',
				'aParams' => $aValidation
			)
		);		
        	if ($aVals = $this->request()->get('val'))
		{
			if ($oValidator->isValid($aVals))
			{
				if ($bIsEdit)
				{
					if (Phpfox::getService('event.process')->update($aEvent['event_id'], $aVals, $aEvent))
					{
						if (isset($aVals['step']))
						{
							if ($aVals['step'] == 'invite')
							{
								$this->url()->send(($aCallback !== false ? $aCallback['url_home'] : 'event.view'), ($aCallback !== false ? array('event', 'view', $aEvent['title_url']) : $aEvent['title_url']), Phpfox::getPhrase('event.successfully_invited_guests_to_this_event'));
							}
							else
							{
								$this->url()->send('event.add', array('id' => $aEvent['event_id'], 'step' => 'invite'));
							}
						}
						else
						{
							switch ($aVals['action'])
							{
								case 'customize':
									Phpfox::getLib('session')->set('event_add_message', Phpfox::getPhrase('event.successfully_customized_this_event'));

									$this->url()->send('event.add.customize', array('id' => $aEvent['event_id']));
									break;
								case 'invite':
									Phpfox::getLib('session')->set('event_add_message', Phpfox::getPhrase('event.successfully_invited_guests_to_this_event'));

									$this->url()->send('event.add.invite', array('id' => $aEvent['event_id']));
									break;
								default:
									Phpfox::getLib('session')->set('event_add_message', Phpfox::getPhrase('event.event_successfully_updated'));

									$this->url()->send('event.add', array('id' => $aEvent['event_id']));
									break;
							}
						}
					}
				}
				else
				{
					if (($iFlood = Phpfox::getUserParam('event.flood_control_events')) !== 0)
					{
						$aFlood = array(
							'action' => 'last_post', // The SPAM action
							'params' => array(
								'field' => 'time_stamp', // The time stamp field
								'table' => Phpfox::getT('event'), // Database table we plan to check
								'condition' => 'user_id = ' . Phpfox::getUserId(), // Database WHERE query
								'time_stamp' => $iFlood * 60 // Seconds);
							)
						);

						// actually check if flooding
						if (Phpfox::getLib('spam')->check($aFlood))
						{
							Phpfox_Error::set(Phpfox::getPhrase('event.you_are_creating_an_event_a_little_too_soon') . ' ' . Phpfox::getLib('spam')->getWaitTime());
						}
					}

					if (Phpfox_Error::isPassed())
					{
						if ($aCallback !== false && $aCallback['view_id'] == '2')
						{
							define('PHPFOX_SKIP_FEED_ENTRY', true);
						}

						if ($iId = Phpfox::getService('event.process')->add($aVals, ($aCallback !== false ? $sModule : 'event'), ($aCallback !== false ? $iItem : 0)))
						{
							Phpfox::getLib('session')->set('event_add_message', Phpfox::getPhrase('event.event_successfully_added') . ((empty($sModule) && Phpfox::getUserParam('event.event_must_be_approved')) ? ' ' . Phpfox::getPhrase('event.note_that_events_must_first_be_approved_by_a_site_admin_before_it_is_displayed_publicly') : ''));

							$this->url()->send('event.add', ($aCallback !== false ? array('module' => $sModule, 'item' => $iItem, 'id' => $iId, 'step' => 'customize') : array('id' => $iId, 'step' => 'customize')));
						}
					}
				}
			}

			$sStep = (isset($aVals['step']) ? $aVals['step'] : '');
			$sAction = (isset($aVals['action']) ? $aVals['action'] : '');
			$this->template()->assign('aForms', $aVals);
		}

		$sEventMessage = '';
		if (($sEventMessage = Phpfox::getLib('session')->get('event_add_message')))
		{
			Phpfox::getLib('session')->remove('event_add_message');
		}
            	$this->template()->setTitle(($bIsEdit ? Phpfox::getPhrase('event.edit_event') . ': ' . $aEvent['title'] : Phpfox::getPhrase('event.create_new_event')))
			->setBreadcrumb(Phpfox::getPhrase('event.events'), $this->url()->makeUrl('event'))
			->setBreadcrumb(($bIsEdit ? Phpfox::getPhrase('event.edit_event') : Phpfox::getPhrase('event.create_new_event')), ($bIsEdit ? $this->url()->makeUrl('event.add', array('id' => $aEvent['event_id'])) : $this->url()->makeUrl('event.add')), ($bIsEdit ? false : true))
			->setEditor()
			->setPhrase(array(
					'core.select_a_file_to_upload'
				)
			)
			->setHeader('cache', array(
					'add.js' => 'module_event',
					'pager.css' => 'style_css',
					'progress.js' => 'static_script',
					'country.js' => 'module_core'
				)
			)
			->setHeader(array(
					'<script type="text/javascript">var oProgressBar = {holder: \'#js_event_block_customize_holder\', progress_id: \'#js_progress_bar\', uploader: \'#js_progress_uploader\', add_more: false, max_upload: 1, total: 1, frame_id: \'js_upload_frame\', file_id: \'image\'};</script>',
					'<script type="text/javascript">$(function(){$(\'#js_event_block_' . ($sAction ? $sAction : ($sStep ? $sStep : 'detail')) . '\').show();});</script>'
				)
                         )
                         ->assign(array(
					'sCreateJs' => $oValidator->createJS(),
					'sGetJsForm' => $oValidator->getJsForm(false),
                                        'bIsEdit' => $bIsEdit,
					'sStep' => $sStep,
					'sEventMessage' => $sEventMessage,
                                        'sAction' => ($sAction ? $sAction : 'detail'),
					'sCategories' => Phpfox::getService('event.category')->get(),
					'aEventErrors' => Phpfox_Error::get(),
                                        'sHost' => ($aCallback !== false ? $aCallback['host'] : ''),
					'sModule' => ($aCallback !== false ? $sModule : ''),
					'iItem' => ($aCallback !== false ? $iItem : ''),
					'aCallback' => $aCallback,
					'iMaxFileSize' => (Phpfox::getUserParam('event.max_upload_size_event') === 0 ? null : Phpfox::getLib('phpfox.file')->filesize((Phpfox::getUserParam('event.max_upload_size_event') / 1024) * 1048576)),
					'bCanSendEmails' => ($bIsEdit ? Phpfox::getService('event')->canSendEmails($aEvent['event_id']) : false),
					'iCanSendEmailsTime' => ($bIsEdit ? Phpfox::getService('event')->getTimeLeft($aEvent['event_id']) : false),
					'sJsEventAddCommand' => (isset($aEvent['event_id']) ? "if (confirm('" . Phpfox::getPhrase('event.are_you_sure', array('phpfox_squote' => true)) . "')) { $('#js_submit_upload_image').show(); $('#js_event_upload_image').show(); $('#js_event_current_image').remove(); $.ajaxCall('event.deleteImage', 'id={$aEvent['event_id']}'); } return false;" : ''),
					'sTimeSeparator' => Phpfox::getPhrase('event.time_separator')
                                    )
			
			);        
    }
    /**
	 * Garbage collector. Is executed after this class has completed
	 * its job and the template has also been displayed.
	 */
	public function clean()
	{
		(($sPlugin = Phpfox_Plugin::get('event.component_controller_add_clean')) ? eval($sPlugin) : false);
	}

}
?>
